<template>
  <div class="app-container">

      
    <!-- 查询表单 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="consumerQuery.name" placeholder="用户名"/>
      </el-form-item>

      <el-form-item>
        <el-select v-model="consumerQuery.role" clearable placeholder="用户权限">
          <el-option :value="0" label="管理员"/>
          <el-option :value="1" label="普通用户(非作者和管理员)"/>
          <el-option :value="2" label="作者"/>
        </el-select>
      </el-form-item>

      <el-form-item label="添加时间">
        <el-date-picker
          v-model="consumerQuery.begin"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="consumerQuery.end"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      element-loading-text="数据加载中"
      :data="list"
      border
      fit
      highlight-current-row>

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="nickname" label="昵称" width="80"  align="center" />
      <el-table-column prop="username" label="用户名" width="80"  align="center" />
      <el-table-column label="性别" width="80"  align="center" >
        <template slot-scope="scope">
          {{ scope.row.sex===1?'女':'男' }} <!-- 得到每行的值进行判断  ===类型也匹配-->
        </template>
      </el-table-column>

      <el-table-column prop="age" label="年龄"  width="80"  align="center" />
      <el-table-column prop="email" label="邮箱" width="180"  align="center" />
      <el-table-column prop="created" label="添加时间" width="160"  align="center"/>
      <el-table-column prop="mobile" label="手机号" width="80"  align="center" />
      <el-table-column label="权限" width="80"  align="center" >
        <template slot-scope="scope">
          {{ scope.row.role==='0'?'admin':(scope.row.role==='1'?'user':'author')}} <!-- 得到每行的值进行判断  ===类型也匹配-->
        </template>
      </el-table-column>
   
      
      <!-- <el-table-column prop="sort" label="排序" width="60" /> -->

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="'/consumer/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
        :current-page="page"
        :page-size="limit"
        :total="total"
        style="padding:30px 0;text-align:center;"
        layout="total,prev,pager,next,jumper"
        @current-change="getList" 
       />

  </div>
</template>

<style>

.tableBox{
    height: 100px;
}

</style>

<script> 
import consumer from '@/api/tingting/consumer'

export default {
    
    data(){
        //定义变量和初始值
        return{
            list:null,
            page:1,
            limit:4,
            total:0,
            consumerQuery:{}
        }
    },
    created(){//在页面渲染之前执行，一般调用methods定义的方法
        this.getList()
    },
    methods:{//创建具体的方法，调用接口中定义的方法
        //用户列表：
        getList(page = 1){
            this.page = page
            consumer.getConsumerListPage(this.page,this.limit,this.consumerQuery)
            .then(response =>{
              this.list=response.data.rows
              this.total=response.data.total
            })//请求成功
            .catch(error => {
                console.log(error)
            })//请求失败
        },
        //清空的方法
        resetData(){

            this.consumerQuery={}
            this.getList()

        },
        //删除的方法
        removeDataById(id){

          this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
          }).then(() => {
            consumer.deleteConsumerId(id)
            .then(response => {
                this.$message({
                type: 'success',
                message: '删除成功!'
          });
                this.getList()
            })
         
         })

        }
    }

}

</script>